<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '活动添加' : '活动修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      user:{{ user }}
      <el-form :model="user">
        <el-form-item label="活动名称" label-width="120px">
          <el-input v-model="user.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动名称" label-width="120px">
          <el-date-picker
            v-model="value1"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="一级分类" label-width="120px">
          <el-select v-model="user.first_cateid" @change="change_first_cate">
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="二级分类" label-width="120px">
          <el-select v-model="user.second_cateid">
            <el-option
              v-for="item in second_cate_list"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品" label-width="120px">
          <el-select v-model="user.goodsid">
            <el-option
              v-for="item in second_cate_list"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  reqseckinfo,
  reqseckedit,
  reqsecklist
} from "../../../http/api";
import { formClose } from "../../../mixins";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      user: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      },
      second_cate_list: [],
    };
  },
  computed: {
    ...mapGetters({
      cateList: "seck/arr",
    }),
  },
  mouted() {
    this.reqseckArr();
  },
  methods: {
    ...mapActions({
      reqCateArr: "seck/reqArr",
      reqArr:"seck/reqArr"
    }),

    opened() {
      this.editor = new E("#editor");
      this.editor.create();
      this.editor.txt.html(this.user.description);
    },

    change_first_cate() {
      this.user.second_cateid = "";
      this.get_second_cate_list();
    },
    get_second_cate_list() {
      reqsecklist({ pid: this.user.first_cateid }).then((res) => {
        if (res.data.code == 200) {
          this.second_cate_list = res.data.list;
        }
      });
    },

    empty() {
      this.user = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
      };
      this.second_cate_list = [];
    },
    add() {
      //处理attrs
      reqseckadd({
        ...this.user,
        specsattr: this.user.specsattr.join(","),
        description: this.editor.txt.html(), //富文本编辑器的内容赋值给description
      }).then((res) => {
        if (res.data.code == 200) {
          // ①弹成功
          successAlert(res.data.msg);
          // ②弹框内容清空
          this.empty();
          // ③弹框消失
          this.info.isshow = false;
          // // ④重新请求列表
          this.reqArr();
        }
      });
    },
    getOne(id){
      reqseckinfo({ id:id }).then((res) =>{
        if(res.data.code === 200){
          this.user = res.data.list;
          this.user.id = id;
          this.get_second_cate_list();
          if(this.editor){
            this.editor.txt.html(this.user.description);
          }
        }
      })
    },
    updata(){
      reqseckedit({
        ...this.user,
        specsattr:this.user.specsattr.join(","),
        description:this.editor.txt.html(),
      }).then((res)=>{
        if (res.data.code === 200){
        successAlert(res.data.msg);
        this.empty();
        this.info.isshow = false;
        this.reqArr();
        }
      })
    }
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.inputAndBtn {
  display: flex;
  .el-button {
    width: auto;
  }
  .el-input {
    flex: 1;
  }
}
</style>
